package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PayOrderDO;
import com.jeequan.jeepay.service.PayOrderService;
import com.jeequan.jeepay.service.mapper.PayOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PayOrderDO;
//import .mapper.PayOrderMapper;
//import .service.PayOrderService;
 /**
 * 订单表;(pay_order)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PayOrderServiceImpl implements PayOrderService {
    @Autowired
    private PayOrderMapper payOrderMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PayOrderDO queryById(Integer id){
        return payOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param payOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PayOrderDO> paginQuery(PayOrderDO payOrder, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PayOrderDO> queryWrapper = new LambdaQueryWrapper<>();
        if(payOrder.getMemberId()!=null){
            queryWrapper.eq(PayOrderDO::getMemberId, payOrder.getMemberId());
        }
        if(payOrder.getBalanceType()!=null){
            queryWrapper.eq(PayOrderDO::getBalanceType, payOrder.getBalanceType());
        }
        if(payOrder.getState()!=null){
            queryWrapper.eq(PayOrderDO::getState, payOrder.getState());
        }
        if(StrUtil.isNotBlank(payOrder.getProductName())){
            queryWrapper.eq(PayOrderDO::getProductName, payOrder.getProductName());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelCode())){
            queryWrapper.eq(PayOrderDO::getChannelCode, payOrder.getChannelCode());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelTitle())){
            queryWrapper.eq(PayOrderDO::getChannelTitle, payOrder.getChannelTitle());
        }
        if(StrUtil.isNotBlank(payOrder.getMerchantNo())){
            queryWrapper.eq(PayOrderDO::getMerchantNo, payOrder.getMerchantNo());
        }
        if(StrUtil.isNotBlank(payOrder.getSignKey())){
            queryWrapper.eq(PayOrderDO::getSignKey, payOrder.getSignKey());
        }
        if(StrUtil.isNotBlank(payOrder.getOrderId())){
            queryWrapper.eq(PayOrderDO::getOrderId, payOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(payOrder.getOutTradeId())){
            queryWrapper.eq(PayOrderDO::getOutTradeId, payOrder.getOutTradeId());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelOrderId())){
            queryWrapper.eq(PayOrderDO::getChannelOrderId, payOrder.getChannelOrderId());
        }
        if(StrUtil.isNotBlank(payOrder.getNotifyUrl())){
            queryWrapper.eq(PayOrderDO::getNotifyUrl, payOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(payOrder.getMobile())){
            queryWrapper.eq(PayOrderDO::getMobile, payOrder.getMobile());
        }
        if(StrUtil.isNotBlank(payOrder.getEmail())){
            queryWrapper.eq(PayOrderDO::getEmail, payOrder.getEmail());
        }
        if(StrUtil.isNotBlank(payOrder.getExtend())){
            queryWrapper.eq(PayOrderDO::getExtend, payOrder.getExtend());
        }
        if(StrUtil.isNotBlank(payOrder.getRemark())){
            queryWrapper.eq(PayOrderDO::getRemark, payOrder.getRemark());
        }
        if(StrUtil.isNotBlank(payOrder.getReturnUrl())){
            queryWrapper.eq(PayOrderDO::getReturnUrl, payOrder.getReturnUrl());
        }
        if(StrUtil.isNotBlank(payOrder.getOriginUrl())){
            queryWrapper.eq(PayOrderDO::getOriginUrl, payOrder.getOriginUrl());
        }
        //2. 执行分页查询
        Page<PayOrderDO> pagin = new Page<>(current , size , true);
        IPage<PayOrderDO> selectResult = payOrderMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param payOrder 实例对象
     * @return 实例对象
     */
    public PayOrderDO insert(PayOrderDO payOrder){
        payOrderMapper.insert(payOrder);
        return payOrder;
    }
    
    /** 
     * 更新数据
     *
     * @param payOrder 实例对象
     * @return 实例对象
     */
    public PayOrderDO update(PayOrderDO payOrder){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PayOrderDO> chainWrapper = new LambdaUpdateChainWrapper<PayOrderDO>(payOrderMapper);
        if(StrUtil.isNotBlank(payOrder.getProductName())){
            chainWrapper.set(PayOrderDO::getProductName, payOrder.getProductName());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelCode())){
            chainWrapper.set(PayOrderDO::getChannelCode, payOrder.getChannelCode());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelTitle())){
            chainWrapper.set(PayOrderDO::getChannelTitle, payOrder.getChannelTitle());
        }
        if(StrUtil.isNotBlank(payOrder.getMerchantNo())){
            chainWrapper.set(PayOrderDO::getMerchantNo, payOrder.getMerchantNo());
        }
        if(StrUtil.isNotBlank(payOrder.getSignKey())){
            chainWrapper.set(PayOrderDO::getSignKey, payOrder.getSignKey());
        }
        if(StrUtil.isNotBlank(payOrder.getOrderId())){
            chainWrapper.set(PayOrderDO::getOrderId, payOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(payOrder.getOutTradeId())){
            chainWrapper.set(PayOrderDO::getOutTradeId, payOrder.getOutTradeId());
        }
        if(StrUtil.isNotBlank(payOrder.getChannelOrderId())){
            chainWrapper.set(PayOrderDO::getChannelOrderId, payOrder.getChannelOrderId());
        }
        if(StrUtil.isNotBlank(payOrder.getNotifyUrl())){
            chainWrapper.set(PayOrderDO::getNotifyUrl, payOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(payOrder.getMobile())){
            chainWrapper.set(PayOrderDO::getMobile, payOrder.getMobile());
        }
        if(StrUtil.isNotBlank(payOrder.getEmail())){
            chainWrapper.set(PayOrderDO::getEmail, payOrder.getEmail());
        }
        if(StrUtil.isNotBlank(payOrder.getExtend())){
            chainWrapper.set(PayOrderDO::getExtend, payOrder.getExtend());
        }
        if(StrUtil.isNotBlank(payOrder.getRemark())){
            chainWrapper.set(PayOrderDO::getRemark, payOrder.getRemark());
        }
        if(StrUtil.isNotBlank(payOrder.getReturnUrl())){
            chainWrapper.set(PayOrderDO::getReturnUrl, payOrder.getReturnUrl());
        }
        if(StrUtil.isNotBlank(payOrder.getOriginUrl())){
            chainWrapper.set(PayOrderDO::getOriginUrl, payOrder.getOriginUrl());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PayOrderDO::getId, payOrder.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(payOrder.getId());
        }else{
            return payOrder;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = payOrderMapper.deleteById(id);
        return total > 0;
    }
}